//
//  LWBaseViewController.m
//  1.28  weibo
//
//  Created by mac on 15/2/1.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWBaseViewController.h"
#import "LWNavController.h"
#import "LWOAuthViewController.h"
#import "LWAccount.h"

#import "LWProDuctItme.h"
#import "LWProductGroup.h"
#import "LWProductCell.h"
#import "LWProductArrowItme.h"

#import "CZTestController.h"
@interface LWBaseViewController () <LWDefaultViewDeledate>

@end

@implementation LWBaseViewController

#pragma mark  创建defaultView的方法
- (void)loadView{

    LWAccount *account = [LWAccount accountFromSandBox];
    if (account != nil) {
        [super loadView];
    }else{
    LWDefaultView *defaultView = [LWDefaultView defaultView];
    self.view = defaultView;
    self.defaultView = defaultView;
    self.defaultView.deledate =self;
    }
}
- (void)defaultView:(LWDefaultView *)defaultView didClickLogin:(UIButton *)loginBt{
    
    //点击登录按钮moda一个授权控制器
    LWNavController *nav = [[LWNavController alloc] init];
    LWOAuthViewController *oauth = [[LWOAuthViewController alloc] init];
    [nav addChildViewController:oauth];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)defaultView:(LWDefaultView *)defaultView didClickRegist:(UIButton *)RegistBtn{
 
}
#pragma mark - tableView的数据源和代理的方法
- (NSMutableArray *)datas{

    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    LWProductGroup *group = self.datas[section];
    return group.itmes.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    LWProductCell *cell = [LWProductCell cellWithTableView:tableView];
    LWProductGroup *group = self.datas[indexPath.section];
    LWProDuctItme *itme = group.itmes[indexPath.row];
    cell.itme = itme;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if (self.datas != nil && self.datas.count > 0) {
        
        LWProductGroup *group = self.datas[section];
        return group.hearderTitle;
    }
    return nil;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    if (self.datas != nil && self.datas.count > 0) {
        LWProductGroup *group = self.datas[section];
        return group.footerTitle;
    }
    return nil;
}

/**
 *  选中某一行的时候
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

        if (self.datas != nil && self.datas.count > 0) {
            
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
            
        LWProductGroup *group = self.datas[indexPath.section];
        LWProDuctItme *itme = group.itmes[indexPath.row];
        if (itme.option != nil) {
            itme.option();
            return;
        }
        if ([itme isKindOfClass:[LWProductArrowItme class]]) {
            LWProductArrowItme *arrowItme = (LWProductArrowItme *)itme;
            
            if (arrowItme.destVCName != nil) {
                
                UIStoryboard *sb = [UIStoryboard storyboardWithName:arrowItme.destVCName bundle:nil];
                UIViewController *vc = [sb instantiateInitialViewController];
                vc.title = arrowItme.title;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                
                CZTestController *test = [[CZTestController alloc] init];
                test.title = arrowItme.title;
                [self.navigationController pushViewController:test animated:YES];
                
            }
    
        }
        
    }
    
}

@end
